﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

using Perseus;
using Perseus.Plugins;

namespace Hetymine.Executioner.Controls {
    /// <summary>
    /// Interaction logic for PluginInfo.xaml
    /// </summary>
    public partial class PluginInfo : UserControl {
        Perseus.Plugins.PluginInfo _Info;

        public PluginInfo() : this (null) { }
        public PluginInfo(Perseus.Plugins.PluginInfo info) {
            InitializeComponent();

            this.cWebsiteUrl.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(WebsiteUrl_RequestNavigate);

            this.Info = info;
        }

        public Perseus.Plugins.PluginInfo Info {
            get { return this._Info; }
            set {
                this._Info = value;
                
                bool info = false;
                
                if (value != null && !value.Author.IsEmpty()) {
                    this.cAuthorLabel.Visibility = System.Windows.Visibility.Visible;
                    this.cAuthor.Visibility = System.Windows.Visibility.Visible;
                    this.cAuthor.Text = value.Author;

                    info = true;
                }
                else {
                    this.cAuthorLabel.Visibility = System.Windows.Visibility.Collapsed;
                    this.cAuthor.Visibility = System.Windows.Visibility.Collapsed;
                }
                
                if (value != null && !value.Website.IsEmpty()) {
                    this.cWebsiteLabel.Visibility = System.Windows.Visibility.Visible;
                    this.cWebsite.Visibility = System.Windows.Visibility.Visible;
                                        
                    string website = value.Website;
                    if (website.IndexOf("://", StringComparison.Ordinal) < 0) {
                        website = "http://" + website;
                    }
                    this.cWebsiteUrl.Inlines.Clear();
                    this.cWebsiteUrl.Inlines.Add(website);
                    this.cWebsiteUrl.NavigateUri = new Uri(website);                    

                    info = true;
                }
                else {
                    this.cWebsiteLabel.Visibility = System.Windows.Visibility.Collapsed;
                    this.cWebsite.Visibility = System.Windows.Visibility.Collapsed;
                }

                if (value != null && !value.Description.IsEmpty()) {                    
                    this.cDescription.Visibility = System.Windows.Visibility.Visible;
                    this.cDescription.Text = value.Description;
                }
                else if (info) {
                    this.cDescription.Visibility = System.Windows.Visibility.Collapsed;                    
                }
                else {
                    this.cDescription.Visibility = System.Windows.Visibility.Visible;
                    this.cDescription.Text = "<no plugin info>";
                }
            }
        }

        private void WebsiteUrl_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) {
            Process.Start(e.Uri.AbsoluteUri);
            e.Handled = true;
        }
    }
}
